using UnityEngine;
using System.Collections;

public class SHIELD : MonoBehaviour {


	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	if(GameObject.FindGameObjectWithTag("SHIELD")==null)	
		{
			Destroy(gameObject);
		}
	}
	void OnCollisionEnter()
	{
		
		Destroy (FindClosestEnemy());	
		}
	
	    GameObject FindClosestEnemy() {
        GameObject[] gos;
        gos = GameObject.FindGameObjectsWithTag("SHIELD");
        GameObject closest=gos[0];
        float distance = 0;
        Vector3 position = transform.position;
        foreach (GameObject go in gos) {
            Vector3 diff = go.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance > distance) {
                closest = go;
                distance = curDistance;
            }
        }
        return closest;
    }
	
}
